#include "TextureImporter.h"

TextureImporter* TextureImporter::m_pInstance = NULL;
TextureImporter::TextureImporter()
{
	ilInit();
}
TextureImporter::~TextureImporter()
{
 	for( std::map<std::string, ILuint>::iterator mit = m_ILTextures.begin();
 		mit != m_ILTextures.end(); mit ++ )
 	{
 		ilDeleteImages( 1, &(*mit).second ); 
 	}

	for( std::map<std::string, GLuint>::iterator mit = m_Textures.begin();
		mit != m_Textures.end(); mit ++ )
	{
		glDeleteTextures( 1, &(*mit).second ); 
	}
}
void TextureImporter::LoadTexture( const std::string& name )
{
	if( ExistTexture(name) )
	{
		return;
	}

	ILuint texid;
	ILboolean success; 
	GLuint image;

	ilGenImages(1, &texid);											/* Generation of one image name */
	ilBindImage(texid);												/* Binding of image name */

	success = ilLoadImage((const ILstring)name.c_str());			/* Loading of image */
	if( success )
	{
		success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);			/* Convert every colour component into unsigned byte. If your image contains alpha channel you can replace IL_RGB with IL_RGBA */
		if( !success )
		{
			std::cerr<< "(TextureImporter) Error: " << "Loading texture " << name << "\n";
			return;
		}

		glGenTextures(1, &image);										/* Texture name generation */
		glBindTexture(GL_TEXTURE_2D, image);							/* Binding of texture name */

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	/* We will use linear
																			interpolation for magnification filter */
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	/* We will use linear
																			interpolation for minifying filter */
		glTexImage2D( GL_TEXTURE_2D, 0, 
			ilGetInteger(IL_IMAGE_BPP), 
			ilGetInteger(IL_IMAGE_WIDTH),
			ilGetInteger(IL_IMAGE_HEIGHT),
			0, 
			ilGetInteger(IL_IMAGE_FORMAT),
			GL_UNSIGNED_BYTE,
			ilGetData() );												/* Texture specification */

		std::cout<< "(TextureImporter) " << "Load texture " << name << "\n";

		m_IL_Data[name]				= ilGetData();
		m_IL_IMAGE_BPP[name]		= ilGetInteger(IL_IMAGE_BPP);
		m_IL_IMAGE_WIDTH[name]		= ilGetInteger(IL_IMAGE_WIDTH);
		m_IL_IMAGE_HEIGHT[name]		= ilGetInteger(IL_IMAGE_HEIGHT);
		m_IL_IMAGE_FORMAT[name]		= ilGetInteger(IL_IMAGE_FORMAT);
		m_ILTextures[name]			= texid;
		m_Textures[name]			= image;
	}
	else
	{
		std::cerr<< "(TextureImporter) Error: " << "Loading texture " << name << "\n";
		return;
	}
}
bool TextureImporter::GetTextureData( const std::string& name, TextureStruct& ts )
{
	if( name.empty() )
	{
		return false;
	}

	LoadTexture(name);
	
	ts.texture			= m_Textures[name];
	ts.iL_Data			= m_IL_Data[name];
	ts.iL_IMAGE_BPP		= m_IL_IMAGE_BPP[name];
	ts.iL_IMAGE_WIDTH	= m_IL_IMAGE_WIDTH[name];
	ts.iL_IMAGE_HEIGHT	= m_IL_IMAGE_HEIGHT[name];
	ts.iL_IMAGE_FORMAT	= m_IL_IMAGE_FORMAT[name];

	return true;
}
bool TextureImporter::ExistTexture( const std::string& name )
{
	if( m_Textures.find( name ) != m_Textures.end()	)
	{
		return true;
	}

	return false;
}